//
//  NewsDeatilWebViewController.m
//  XWZ_News_Work
//
//  Created by dllo on 16/1/18.
//  Copyright © 2016年 dllo. All rights reserved.
//

#import "NewsDeatilWebViewController.h"

@interface NewsDeatilWebViewController ()<UIWebViewDelegate>
@property (nonatomic, strong) UIWebView *webView;
@property (nonatomic, strong) NSMutableArray *imageArr;
@property (nonatomic, assign) NSInteger height;

@end

@implementation NewsDeatilWebViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    self.webView = [UIWebView new];
    [self.view addSubview:self.webView];
    [self.webView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.edges.equalTo(self.view);
        
    }];
    NSString *htmlStr = [NSString stringWithFormat:@"http://c.3g.163.com/nc/article/%@/full.html", self.docId];
    self.webView.delegate = self;
    [self loadDetailDataWithURLStr:htmlStr];
    
    
    
}

- (void)loadDetailDataWithURLStr:(NSString *)str
{
    NSString *url_string = [str stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
    [xwzAFNetTool getNetWithURL:url_string body:nil headFile:nil responStyle:(XWZ_JSON) success:^(NSURLSessionDataTask *task, id responseObject) {
        NSDictionary *dic = [responseObject objectForKey:self.docId];
        NSMutableString *bodyStr = [NSMutableString string];
        bodyStr = [dic objectForKey:@"body"];
        // 添加图片
        // 数组初始化
        NSMutableArray *tempArr = [NSMutableArray array];
        if ([[dic allKeys] containsObject:@"img"]) {
            tempArr = [dic objectForKey:@"img"];
            for (NSDictionary *refDic in tempArr) {
                for (NSString *key in refDic) {
                    if ([[refDic allKeys] containsObject:@"pixel"] && ![[refDic valueForKey:@"pixel"] isEqualToString:@""]) {
                        NSString *pixelS = [NSString stringWithFormat:@"%@", [refDic objectForKey:@"pixel"]];
                        NSArray *pixelArr = [pixelS componentsSeparatedByString:@"*"];
                        NSString *widthS = [pixelArr firstObject];
                        NSString *heightS = [pixelArr lastObject];
                        
                        NSInteger width = widthS.integerValue;
                        NSInteger height = heightS.integerValue;
                        
                        self.height = self.view.frame.size.width * height / width;
                        
                        
                    }
                    
                    if (![[refDic allKeys] containsObject:@"pixel"]) {
                        self.height = 450;
                    }
                    
                    // 通过key获取src数据
                    if ([key isEqualToString:@"src"]) {
                        // 取出ref
                        NSRange range = [bodyStr rangeOfString:[refDic objectForKey:@"ref"]];
                        if (range.length != NSNotFound) {
                            NSString *picStr = [NSString stringWithFormat:@"<img src=\"%@\" width=\"355\" height=\"%ld\">", [refDic objectForKey:key], self.height];
                            bodyStr = [bodyStr stringByReplacingCharactersInRange:range withString:picStr].mutableCopy;
                        }
                        
                        
                        
                        
                    }
                    
                    
                    
                }
            }
        }
        
        
        NSMutableArray *videoArr = [NSMutableArray array];
        /* 添加视频 */
        if ([[dic allKeys] containsObject:@"video"]) {
            videoArr = [dic objectForKey:@"video"];
            
            
            for (NSDictionary *refDic in videoArr) {
                
                
                for (NSString *key in refDic) {
                    
                    
                    if ( [key  isEqualToString:@"url_mp4"] && ![[refDic valueForKey:@"url_mp4"] isEqualToString:@""] ) {
                        
                        
                        NSRange range1 = [bodyStr rangeOfString:[refDic objectForKey:@"ref"]];
                        
                        
                        if (range1.length != NSNotFound) {
                            
                            
                            NSString *videoStr = [NSString stringWithFormat:@"<video src=\"%@\" width=\"355\" height=\"240\" webkit-playsinline controls=\"preload\"></video>",[refDic objectForKey:key]];
                            
                            
                            bodyStr = [bodyStr stringByReplacingCharactersInRange:range1 withString:videoStr].mutableCopy;
                        }
                    }
                }
            }
        }
        
        [self.webView loadHTMLString:bodyStr baseURL:nil];
        
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        
    }];
    
}

- (void)webViewDidStartLoad:(UIWebView *)webView
{
    NSLog(@"加载开始");
}
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
     NSLog(@"加载结束");
}
- (void)webView:(UIWebView *)webView didFailLoadWithError:(nullable NSError *)error
{
    NSLog(@"加载错误");
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
